srvMap.add('selectFolderInfo', 'marketFolderInfo.json','front/sh/indicatorStore!execute?uid=store_001');   //查询folder数据，提供展示
srvMap.add('updateUserFolder', 'updateUserFolder.json','front/sh/indicatorStore!execute?uid=store_008');   //更新用户选择的一级指标分类以及分类顺序

//测试数据，后期从session中获取
var USER_ID = "1234"; 

var level1Array = new Array();
var level2Array = new Array();
var level3Array = new Array();

var loadFolderInfoAll = [];

/**
 * Folder
 * @author suyao
 */
Load_Folder = {
		/**
		 * 加载页面
		 */
		loadIndexFolderInfo : function(){
			var par = "&userId="+USER_ID;
			Util.ajax.postJsonSync(srvMap.get('selectFolderInfo'),par,function(json,status){
				if(status){
					loadFolderInfoAll = json.object.FOLDER_INFO;
					Load_Folder.loadTotleFolder(json.object);
//					loadUserFolder(json.object);
				}else{
					 $.messager.alert('错误','数据异常！');   
				}
			});
		},
		/**
		 * 根据id查找递归查找其子节点，拼接成字符串，逗号“,”分割
		 * @param folderId
		 * 说明：folder层级最多三层，不使用递归
		 */
		selectFolderChildId : function(folderId){
//			loadFolderInfoAll
			var folderIdStr = "";
			var folderIdStrArray = [];
			var folderIdStrArrayTemp = [];
			for(var i=0;loadFolderInfoAll.length>i;i++){
				if(loadFolderInfoAll[i].parentId == folderId){
					folderIdStrArray.push(loadFolderInfoAll[i].id);
					folderIdStrArrayTemp.push(loadFolderInfoAll[i].id);
				}
			}
			
			for(var m=0;folderIdStrArrayTemp.length>m;m++){
				for(var n=0;loadFolderInfoAll.length>n;n++){
					if(folderIdStrArrayTemp[m] == loadFolderInfoAll[n].parentId){
						folderIdStrArray.push(loadFolderInfoAll[n].id);
					}
				}
			}
			
			for(var j=0;folderIdStrArray.length>j;j++){
				folderIdStr += folderIdStrArray[j]+',';
			}
			folderIdStr += folderId;
//			alert(folderIdStr)
			return folderIdStr;
		},
		/**
		 * 加载全部分类
		 * @param obj
		 */
		loadTotleFolder : function(obj){
			var folderArray = [];
			folderArray = obj.FOLDER_INFO;
			var folderLength = folderArray.length;
			
			for(var i = 0;i < folderLength;i++){
				var level = folderArray[i].level;
				if(level == "1"){
					var level1Node = new Object();
					level1Node.id = folderArray[i].id ;
					level1Node.name = folderArray[i].name ;
					level1Array.push(level1Node);
				}
				if(level == "2"){
					var level2Node = new Object();
					level2Node.id = folderArray[i].id ;
					level2Node.name = folderArray[i].name ;
					level2Node.parentId = folderArray[i].parentId;
					level2Array.push(level2Node);
				}
				if(level == "3"){
					var level3Node = new Object();
					level3Node.id = folderArray[i].id ;
					level3Node.name = folderArray[i].name ;
					level3Node.parentId = folderArray[i].parentId;
					level3Array.push(level3Node);
				}
			}
			Util.ajax.loadTemp('#folderHead',$('#IDX_FLODER_HEADER'),level1Array);
			
			var array1length = level1Array.length;
			var array2length = level2Array.length;
			var array3length = level3Array.length;
			
			for(var i = 0;i<array2length;i++){
				var id = level2Array[i].id;
				var child = [];
				for(var j=0;j<array3length;j++){
					var pId = level3Array[j].parentId;
					if(id == pId){
						child.push(level3Array[j]);
					}
				}
				level2Array[i].child = child;
			}
			
			for(var m = 0;m<array1length;m++){
				var id = level1Array[m].id;
				var child = [];
				for(var n=0;n<array2length;n++){
					var pId = level2Array[n].parentId;
					if(id == pId){
						child.push(level2Array[n]);
					}
				}
				level1Array[m].child = child;
			}
			Util.ajax.loadTemp('#subplace',$('#IDX_FLODER_LEVELTWO'),level1Array);
			
			//加载用户自定义
			var userFolderArray = [];
			if(obj.USER_FOLDER != undefined){
				userFolderArray = obj.USER_FOLDER;
				var userFolderLength = userFolderArray.length;
				var userArrayDit = [];
				for(var p = 0;p<userFolderLength;p++){
					var idUser = userFolderArray[p].folderId;
					for(var q = 0;q<array1length;q++){
						var id = level1Array[q].id;
						if(idUser == id){
							userArrayDit.push(level1Array[q]);
						}
					}
				}
				Util.ajax.loadTemp('#userFolder',$('#IDX_FLODER_USER'),userArrayDit);
			}
		},
		/**
		 * 更新用户自选folder层级
		 */
		updateUserFolder : function(){
			//生成userFolder更新数组
			var userFolderArray = [];
			var orderNo = 0;
			$("#userFolder").find("li").each(function(){
				var folderObj = new Object();
				orderNo++;
				var attenFolderName =  $(this).find('p').text();
				
				attenFolderName = encodeURIComponent(attenFolderName);   
				attenFolderName = attenFolderName.replace(/%5C/g, "%5C%5C");
				attenFolderName = attenFolderName.replace(/%22/g, "%5C%22");
				attenFolderName = attenFolderName.replace(/%0A/g, "");
				attenFolderName = attenFolderName.replace(/%25/g, "%5C%25");
				
				var attenFolderId = $(this).find('p').attr('id');
				
				if(attenFolderName != "" && attenFolderName != undefined && attenFolderId != "" && attenFolderId != undefined){
					folderObj.folderName = attenFolderName;
					folderObj.folderId = attenFolderId;
					folderObj.orderNo = orderNo;
					
					userFolderArray.push(folderObj);
				}
			});
			userFolderArray = JSON.stringify(userFolderArray);
			
			var par = "&userId="+USER_ID+"&userFolderArray="+userFolderArray;
//			alert(par)
			Util.ajax.postJsonSync(srvMap.get('updateUserFolder'),par,function(json,status){
				if(!status){
					$.messager.alert('错误','更新失败！');  
				}
			});
		},
		/**
		 * 移除tip
		 * @param obj
		 */
		removeUserTip : function(obj){
			$(obj).parent().remove();
			Load_Folder.updateUserFolder();
		}
}

/**
 * 页面加载
 */
$(document).ready(function(){
	
	Load_Folder.loadIndexFolderInfo();
//	Load_Folder.selectFolderChildId('1');
	$(function(){
		$('.dragitem').draggable({
			revert:true,
			proxy:'clone',
			cursor: 'pointer'
		});
		$('.tabInd').droppable({
			onDragEnter:function(){
				$(this).addClass('over');
			},
			onDragLeave:function(){
				$(this).removeClass('over');
			},
			onDrop:function(e,source){
				$(this).removeClass('over');
				var moveId = $(source).attr("id");
				var moveName = $(source).text();
				var folderArray = [];
				var flag = 0;
				$("#userFolder").find("li").each(function(){
					var id = $(this).find("p").attr("id");
					var name = $(this).find("p").text();
					if(id != undefined){
						if(moveId != id){
							flag += 0; 
						}else if(moveId == id){
							flag += 1;
						}
						
						var jsonObj = {};
						jsonObj.id = id;
						jsonObj.name = name;
						
						folderArray.push(jsonObj);
						
					}
					
				});
				if(flag == 0){
					var jsonObj = {};
					jsonObj.id = moveId;
					jsonObj.name = moveName;
					folderArray.push(jsonObj);
					//加载用户选项
					var userFolderLength = folderArray.length;
					var userArrayDit = [];
					for(var p = 0;p<userFolderLength;p++){
						var idUser = folderArray[p].id;
						for(var q = 0;q<level1Array.length;q++){
							var id = level1Array[q].id;
							if(idUser == id){
								userArrayDit.push(level1Array[q]);
							}
						}
					Util.ajax.loadTemp('#userFolder',$('#IDX_FLODER_USER'),userArrayDit);
					
					//更新用户自定义标签数据
					Load_Folder.updateUserFolder();
					}
				}
					
			}
		});
	});
});
